﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using Autodesk.Revit.DB;
using QuickModel.InteractiveOperation;
using Teigha.Core;
using Teigha.TD;
using Transfer.Utils;

namespace QuickModel.LastFastModeling
{
	public class PointPosBlockInfo : BlockInfo
	{
		public List<XYZ> ActualBox
		{
			get
			{
				return this.m_ptActualBox;
			}
		}

		public List<Curve> LstCurves
		{
			get
			{
				return this.m_lstCurves;
			}
		}

		public PointPosBlockInfo(OdDbBlockReference odDbBlockRef, ImportInstance ImportIns) : base(odDbBlockRef, ImportIns)
		{
			this.GetActualBox();
		}

		private void GetActualBox()
		{
			try
			{
				OdDbBlockReference odDbBlockReference = this.m_odDbBlockRef.clone() as OdDbBlockReference;
				bool flag = odDbBlockReference == null;
				if (!flag)
				{
					odDbBlockReference.setRotation(0.0);
					OdGeExtents3d odGeExtents3d = new OdGeExtents3d();
					bool flag2 = odDbBlockReference.getGeomExtents(odGeExtents3d) > OdResult.eOk;
					if (!flag2)
					{
						OdGePoint3d odGePoint3d = odGeExtents3d.minPoint();
						OdGePoint3d odGePoint3d2 = odGeExtents3d.maxPoint();
						OdGePoint3d item = new OdGePoint3d(odGePoint3d.x, odGePoint3d2.y, 0.0);
						OdGePoint3d item2 = new OdGePoint3d(odGePoint3d2.x, odGePoint3d.y, 0.0);
						List<OdGePoint3d> list = new List<OdGePoint3d>();
						list.Add(odGePoint3d);
						list.Add(item);
						list.Add(odGePoint3d2);
						list.Add(item2);
						double angle = this.m_odDbBlockRef.rotation();
						OdGeScale3d odGeScale3d = this.m_odDbBlockRef.scaleFactors();
						OdGePoint3d center = this.m_odDbBlockRef.position();
						OdGeMatrix3d odGeMatrix3d = OdGeMatrix3d.rotation(angle, OdGeVector3d.kZAxis, center);
						OdGeMatrix3d xfm = odGeMatrix3d;
						List<OdGePoint3d> list2 = new List<OdGePoint3d>();
						foreach (OdGePoint3d odGePoint3d3 in list)
						{
							list2.Add(odGePoint3d3.transformBy(xfm));
						}
						bool flag3 = odGeScale3d.sx < 0.0;
						if (flag3)
						{
							OdGePoint3d value = list2[0];
							list2[0] = list2[3];
							list2[3] = value;
							value = list2[1];
							list2[1] = list2[2];
							list2[2] = value;
						}
						bool flag4 = odGeScale3d.sy < 0.0;
						if (flag4)
						{
							OdGePoint3d value2 = list2[0];
							list2[0] = list2[1];
							list2[1] = value2;
							value2 = list2[3];
							list2[3] = list2[2];
							list2[2] = value2;
						}
						Transform totalTransform = base.HostImportIns.GetTotalTransform();
						List<XYZ> list3 = new List<XYZ>();
						foreach (OdGePoint3d cadPnt in list2)
						{
							list3.Add(GeometryCommon.ConvetPointFromCadToRevit(cadPnt, totalTransform));
						}
						this.m_ptActualBox.AddRange(list3);
					}
				}
			}
			catch (Exception ex)
			{
				Trace.Assert(true, ex.Message);
			}
		}

		public void GetCurves()
		{
			try
			{
				List<OdDbCurve> list = new List<OdDbCurve>();
				OdGeMatrix3d xfm = this.m_odDbBlockRef.blockTransform();
				OdDbBlockTableRecord odDbBlockTableRecord = (OdDbBlockTableRecord)this.m_odDbBlockRef.blockTableRecord().openObject(OpenMode.kForRead);
				OdDbObjectIterator odDbObjectIterator = odDbBlockTableRecord.newIterator();
				odDbObjectIterator.start();
				while (!odDbObjectIterator.done())
				{
					try
					{
						OdDbEntity odDbEntity = odDbObjectIterator.entity(OpenMode.kForRead, false);
						bool flag = odDbEntity == null || odDbEntity.visibility() == Visibility.kInvisible;
						if (!flag)
						{
							bool flag2 = odDbEntity is OdDbPolyline;
							if (flag2)
							{
								OdDbPolyline odDbPolyline = OdDbPolyline.cast(odDbEntity);
								List<OdDbLine> list2 = new List<OdDbLine>();
								OdRxObjectPtrArray odRxObjectPtrArray = new OdRxObjectPtrArray();
								bool flag3 = odDbPolyline.subExplode(odRxObjectPtrArray) == OdResult.eOk;
								if (flag3)
								{
									foreach (OdRxObject pObj in odRxObjectPtrArray)
									{
										OdDbLine odDbLine = OdDbLine.cast(pObj);
										bool flag4 = odDbLine != null;
										if (flag4)
										{
											OdDbLine odDbLine2 = (OdDbLine)odDbLine.clone();
											bool flag5 = odDbLine2.transformBy(xfm) == OdResult.eOk;
											if (flag5)
											{
												list2.Add(odDbLine2);
											}
										}
									}
								}
								list.AddRange(list2);
							}
							else
							{
								bool flag6 = odDbEntity is OdDbCurve;
								if (flag6)
								{
									OdDbCurve odDbCurve = odDbEntity as OdDbCurve;
									OdDbCurve odDbCurve2 = odDbCurve.clone() as OdDbCurve;
									bool flag7 = odDbCurve2.transformBy(xfm) == OdResult.eOk;
									if (flag7)
									{
										list.Add(odDbCurve2);
									}
								}
							}
						}
					}
					catch (Exception ex)
					{
						Trace.Assert(true, ex.Message);
					}
					IL_17F:
					odDbObjectIterator.step();
					continue;
					goto IL_17F;
				}
				foreach (OdDbCurve cadCurve in list)
				{
					Curve curve = GeometryCommon.ConvetCurveFromCadToRevit(cadCurve, base.HostImportIns.GetTotalTransform());
					bool flag8 = curve != null;
					if (flag8)
					{
						bool flag9 = curve is Ellipse;
						if (flag9)
						{
							Arc item = null;
							bool flag10 = this.ConvertEllipseToArc(curve as Ellipse, out item);
							if (flag10)
							{
								this.m_lstCurves.Add(item);
							}
						}
						else
						{
							this.m_lstCurves.Add(curve);
						}
					}
				}
			}
			catch (Exception ex2)
			{
				Trace.Assert(true, ex2.Message);
			}
		}

		private bool ConvertEllipseToArc(Ellipse ellipse, out Arc arc)
		{
			arc = null;
			bool flag = !ellipse.IsBound && Geometry.IsEqual(ellipse.RadiusX, ellipse.RadiusY);
			if (flag)
			{
				arc = Arc.Create(ellipse.Center, ellipse.RadiusX, 0.0, Math.PI*2, XYZ.BasisX, XYZ.BasisY);
				bool flag2 = arc != null;
				if (flag2)
				{
					return true;
				}
			}
			return false;
		}

		private List<XYZ> m_ptActualBox = new List<XYZ>();

		private List<Curve> m_lstCurves = new List<Curve>();
	}
}
